package cn.hx.controller;

import cn.hx.entity.User;
import cn.hx.entity.UserSchedule;
import cn.hx.service.NewsService;
import cn.hx.service.UserScheduleService;
import cn.hx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserInfo {

    @Autowired
    private NewsService newsService;
    @Autowired
    private UserScheduleService userScheduleService;

//    日程管理—新增员工日程
    @GetMapping("createUserSchedule")
    public String getCreateUserSchedule(){
        return "createUserSchedule";
    }
//    新增员工提交
    @PostMapping("saveUserSchedule")
    public String getCreateUserSchedule(UserSchedule userSchedule,HttpSession session){
        User user = (User)session.getAttribute("loginUser");
        userSchedule.setUserId(user.getId());
        userScheduleService.insert(userSchedule);
        return "redirect:/searchUserSchedule";
    }

//    日程管理—查询员工日程
    @GetMapping("searchUserSchedule")
    public String getSearchUserSchedule(Model model){
        List<UserSchedule> userSchedules = userScheduleService.selectAll();
        model.addAttribute("userSchedules",userSchedules);
        return "searchUserSchedule";
    }
//    员工日程编辑回显
    @GetMapping("updateUserSchedule/{id}")
    public String getUpdateUserSchedule(@PathVariable Integer id,Model model){
        UserSchedule userSchedule = userScheduleService.queryById(id);
        model.addAttribute("userSchedule",userSchedule);
        return "updateUserSchedule";
    }
//    员工日程修改
    @PostMapping("editUserSchedule/{id}")
    public String postEditUserSchedule(@PathVariable Integer id,UserSchedule userSchedule){
        userSchedule.setId(id);
        userScheduleService.update(userSchedule);
        return "redirect:/searchUserSchedule";
    }

//    员工日程删除
    @GetMapping("deleteUserSchedule/{id}")
    public String getDeleteUserSchedule(@PathVariable Integer id){
        userScheduleService.deleteById(id);
        return "redirect:/searchUserSchedule";
    }



//    文档管理
    @GetMapping("searchMyDoc")
    public String getSearchMyDoc(Model model,HttpSession session){
        User user =(User) session.getAttribute("loginUser");
        model.addAttribute("user",user);
        return "searchMyDoc";
    }
//    公告管理
    @GetMapping("searchNewsInfo")
    public String getSearchNewsInfo(Model model){
        model.addAttribute("news",newsService.findAll());
        return "searchNewsInfo";
    }




}
